// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.warehouse.dbmanager.ui.provider;

import org.dengues.commons.IDenguesSharedImage;
import org.dengues.commons.utils.ImageUtil;
import org.dengues.core.metadata.DatabaseContainer;
import org.dengues.core.metadata.MetadataColumn;
import org.dengues.core.metadata.MetadataDatabase;
import org.dengues.core.metadata.MetadataTable;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.graphics.Image;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2008-1-7 qiang.zhang $
 * 
 */
public class DatabaseNavigateProvider extends LabelProvider implements ITreeContentProvider {

    private final Image tableImage = ImageUtil.getImage(IDenguesSharedImage.VIEW_TABLE);

    private final Image columnImage = ImageUtil.getImage(IDenguesSharedImage.VIEW_COLUMN);

    private final Image databaseImage = ImageUtil.getImage(IDenguesSharedImage.VIEW_CONNECTION);

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.LabelProvider#getImage(java.lang.Object)
     */
    @Override
    public Image getImage(Object element) {
        if (element instanceof MetadataDatabase) {
            return databaseImage;
        } else if (element instanceof MetadataTable) {
            return tableImage;
        } else if (element instanceof MetadataColumn) {
            return columnImage;
        }
        return super.getImage(element);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
     */
    @Override
    public String getText(Object element) {
        if (element instanceof MetadataDatabase) {
            return ((MetadataDatabase) element).getDatabaseName();
        } else if (element instanceof MetadataTable) {
            return ((MetadataTable) element).getTableName();
        } else if (element instanceof MetadataColumn) {
            return ((MetadataColumn) element).getColumnName();
        }
        return super.getText(element);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object,
     * java.lang.Object)
     */
    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
     */
    public Object[] getChildren(Object element) {
        if (element instanceof MetadataDatabase) {
            return ((MetadataDatabase) element).getTables().toArray(new MetadataTable[0]);
        } else if (element instanceof MetadataTable) {
            return ((MetadataTable) element).getColumns().toArray(new MetadataColumn[0]);
        } else if (element instanceof DatabaseContainer) {
            return ((DatabaseContainer) element).getDatabases().toArray(new MetadataDatabase[0]);
        }
        return new Object[0];
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
     */
    public Object getParent(Object element) {
        if (element instanceof MetadataDatabase) {
            return ((MetadataDatabase) element).getDatabaseContainer();
        } else if (element instanceof MetadataTable) {
            return ((MetadataTable) element).getMetadataDatabase();
        } else if (element instanceof DatabaseContainer) {
            return element;
        } else if (element instanceof MetadataColumn) {
            return ((MetadataColumn) element).getMetadataTable();
        }
        return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
     */
    public boolean hasChildren(Object element) {
        return getChildren(element).length > 0;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
     */
    public Object[] getElements(Object inputElement) {
        return getChildren(inputElement);
    }

}
